﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_CancelSignINDoc]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_CancelSignINDoc];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_CancelSignINDoc]
    @StaffName nvarchar(300),
    @DocID int
AS
DECLARE @staff_id int
DECLARE @PassNum int
SELECT @staff_id = staff_id 
    FROM uds_staff
    WHERE staff_name = @StaffName
IF @staff_id>0
BEGIN
    UPDATE uds_flow_status
        SET status =0 
        WHERE Doc_id = @DOCID 
         and  staff_id = @staff_id

    --得到汇签规则
    SELECT     @PassNum = e.PassNum 
        FROM     uds_flow_status c,
            uds_flow_document d,
            uds_flow_step e
        WHERE         c.doc_id      = d.doc_id 
              and d.flow_id     = e.flow_id 
              and d.step_id     = e.step_id 
              and c.doc_id     = @docid


    IF @PassNum = 0
    BEGIN
    --没有汇签,刷新到未签收状态
        UPDATE uds_flow_document 
            SET doc_status =0
            WHERE doc_id = @DocID 
    END
    ELSE
    BEGIN
    --多人汇签，没有一个签收或审批的，刷新到未签收状态
        IF NOT EXISTS(SELECT 1 FROM uds_flow_status WHERE status>0 AND doc_id = @DocID)
            UPDATE uds_flow_document 
                SET doc_status =0
                WHERE doc_id = @DocID 
    END

    RETURN 0        
END
ELSE
    RETURN -1